import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;

public class SubPanels {
    /**
     * 鲁滨逊遇到海难时转场用
     * @param panel         游戏面板
     */
    static void transitionPanel(JPanel panel){
        BufferedImage disaster_img = null;
        java.net.URL disaster_imgURL = SubPanels.class.getResource("imgs/disaster.png");
        try {
            disaster_img = ImageIO.read(new URL(disaster_imgURL.toString()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        ImagePanel disaster_bg = new ImagePanel(disaster_img);

        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                panel.removeAll();
                panel.repaint();
                panel.add(disaster_bg);
                panel.repaint();
                try {
                    Thread.sleep(4000);
                } catch (InterruptedException interruptedException) {
                    interruptedException.printStackTrace();
                }
                panel.remove(disaster_bg);
                panel.repaint();
            }
        });
        t.start();  // 启动线程
    }

    /**
     * 用于显示鲁滨逊活动的信息
     * @param panel     游戏面板
     * @param bg_before 需要被替换的背景
     * @param bg_after  新的背景
     * @param message   给用户的提示信息
     * @param stay      鲁滨逊的静止状态
     * @param activity  鲁滨逊的活动状态
     * @param time      显示持续时间
     */
    static void tipPanel(JPanel panel, ImagePanel bg_before, ImagePanel bg_after, String message,JLabel stay, JLabel activity, int time, Friday friday, int flag){
        JLabel label = new JLabel();
        label.setBounds(450, 400, 200, Settings.save_button_h);
        panel.add(label);
        java.net.URL friday_imgURL = SubPanels.class.getResource("imgs/friday.gif");
        JLabel friday_label = new JLabel(new ImageIcon(friday_imgURL));
        friday_label.setBounds(0, 0, Settings.frame_w, Settings.frame_h);
        int index;
        if(flag == 0){
            index = 3;
        }
        else index = flag -1;
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                if(bg_before!=null)
                    panel.remove(bg_before);
                panel.repaint();
                label.setText(message);
                if( friday.is_work.get(index) == 1)
                    panel.add(friday_label);
                if (activity != null)
                    panel.add(activity);
                panel.add(bg_after);
                panel.repaint();
                try {
                    Thread.sleep(time);  // 该线程睡眠time毫秒
                } catch (InterruptedException interruptedException) {
                    interruptedException.printStackTrace();
                }
                label.setText("");  // 清空文字
                if (activity != null)
                    panel.remove(activity);
                panel.remove(bg_after);
                panel.remove(friday_label);
                if (stay!=null)
                    panel.add(stay);
                if (bg_before!=null)
                    panel.add(bg_before);
                panel.repaint();
            }
        });
        t.start();  // 启动线程
    }
    /**
     * 帮助页面，用于提示用户进行游戏
     * @param panel         游戏面板
     */
    static void helpPanel(JPanel panel){
        new JScrollPaneTest(panel);
    }
    /**
     * 为星期五安排工作的面板。
     * 通过点击对应次数的按钮为星期五安排工作。
     * 点击一次可安排工作，再点击一次可取消，以此类推
     *
     * @param friday    星期五实例，存储玩家所作的修改
     */
    static void arrangeWork(int flag,Friday friday){
        Object[] options = {"yes","no"};
        int response = JOptionPane.showInternalOptionDialog(null,"Would you like Friday to work together","arr",JOptionPane.YES_OPTION,
                JOptionPane.QUESTION_MESSAGE,null,options,options[0]);
        if(response==0)           //一起工作
        {
            friday.is_work.set(flag, 1);
        }
        else
        {
            friday.is_work.set(flag, 0);
        }
    }

    /**
     * 鲁滨逊遇到海难
     */
    static void disasterFrame() {
        System.out.println("hainan");
        JFrame frame = new JFrame("Shipwreck");
        frame.setBounds(0,0, Settings.frame_w, Settings.frame_h);
        frame.setLayout(null);
        frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        JPanel panel = new JPanel();
        panel.setBounds(0,0, Settings.frame_w, Settings.frame_h);
        panel.setLayout(null);
        frame.add(panel);
        frame.setVisible(true);

        JLabel label = new JLabel();
        label.setBounds(250, 200, 600, Settings.save_button_h);
        label.setText("Oh,no!!!It's terrible. We've been shipwrecked!");
        label.setForeground(Color.WHITE);
        label.setFont(new java.awt.Font(Settings.button_font, Font.BOLD, Settings.getFont_size() + 3));

        BufferedImage disaster_img = null;
        java.net.URL disaster_imgURL = SubPanels.class.getResource("imgs/disaster.png");
        try {
            disaster_img = ImageIO.read(new URL(disaster_imgURL.toString()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        ImagePanel disaster = new ImagePanel(disaster_img);
        disaster.setBounds(0,0, Settings.frame_w,Settings.frame_h);
        java.net.URL island_imgURL = SubPanels.class.getResource("imgs/island.png");
        JLabel island = new JLabel(new ImageIcon(island_imgURL));
        island.setBounds(0,0, Settings.frame_w, Settings.frame_h);

        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                panel.repaint();
                panel.add(label);
                panel.add(disaster);
                panel.repaint();
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException interruptedException) {
                    interruptedException.printStackTrace();
                }
                panel.remove(disaster);
                label.setText("Look, there's an island ahead! I'm saved!");
                panel.add(island);
                panel.repaint();
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException interruptedException) {
                    interruptedException.printStackTrace();
                }
                label.setText("");
                panel.repaint();
                frame.dispose();
            }
        });
        t.start();  // 启动线程
    }

    /**
     * 鲁滨逊在散步时看到了野人的痕迹
     */
    static void traceFrame() {
        JFrame frame = new JFrame();
        frame.setBounds(0,0, Settings.frame_w, Settings.frame_h);
        frame.setLayout(null);
        frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        JPanel panel = new JPanel();
        panel.setBounds(0,0, Settings.frame_w, Settings.frame_h);
        panel.setLayout(null);
        frame.add(panel);
        frame.setVisible(true);

        JLabel label = new JLabel();
        label.setBounds(180, 30, 600, Settings.save_button_h);
        label.setText("Oh, my God, I saw the traces of the savage holding a human feast");
        label.setForeground(Color.BLACK);
        label.setFont(new java.awt.Font(Settings.button_font, Font.BOLD, Settings.getFont_size() ));

        BufferedImage hangout = null;
        java.net.URL hangout_imgURL = SubPanels.class.getResource("imgs/hangout_bg.jpg");
        try {
            hangout = ImageIO.read(new URL(hangout_imgURL.toString()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        ImagePanel hangout_bg = new ImagePanel(hangout);

        java.net.URL savage_imgURL = SubPanels.class.getResource("imgs/savage.gif");
        JLabel trace = new JLabel(new ImageIcon(savage_imgURL));
        trace.setBounds(0,0, Settings.frame_w, Settings.frame_h);

        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                panel.repaint();
                panel.add(label);
                panel.add(trace);
                panel.add(hangout_bg);
                panel.repaint();
                try {
                    Thread.sleep(4500);
                } catch (InterruptedException interruptedException) {
                    interruptedException.printStackTrace();
                }
                label.setText("");
                panel.repaint();
                frame.dispose();
            }
        });
        t.start();
    }

    /**
     * 鲁滨逊解锁新人物星期五
     * @param robinson 鲁滨逊实例
     * @return 是否成功
     */
    static boolean saveFridayFrame(Robinson robinson) {
        JFrame frame = new JFrame();
        frame.setBounds(0, 0, Settings.frame_w, Settings.frame_h);
        frame.setLayout(null);
        frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        JPanel panel = new JPanel();
        panel.setBounds(0, 0, Settings.frame_w, Settings.frame_h);
        panel.setLayout(null);
        frame.add(panel);
        frame.setVisible(true);

        JLabel label = new JLabel();
        label.setBounds(450, 100, 600, Settings.save_button_h);
        label.setText("save Friday");
        label.setForeground(Color.WHITE);
        label.setFont(new java.awt.Font(Settings.button_font, Font.BOLD, Settings.getFont_size() + 3));

        BufferedImage save_friday = null;
        java.net.URL save_friday_imgURL = SubPanels.class.getResource("imgs/savefriday.png");
        try {
            save_friday = ImageIO.read(new URL(save_friday_imgURL.toString()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        ImagePanel friday = new ImagePanel(save_friday);

        friday.setBounds(0, 0, Settings.frame_w, Settings.frame_h);

        java.net.URL fight_imgURL = SubPanels.class.getResource("imgs/fight.gif");
        JLabel fight = new JLabel(new ImageIcon(fight_imgURL));
        fight.setBounds(0, 0, Settings.frame_w, Settings.frame_h);
        System.out.println("save friday");
        // 计算成功率
        boolean success = (robinson.isAlert() ? 1 : 0) * 0.7 + (robinson.getIntelligence() - 70.0) / 100.0
                + Math.random() * 0.3 > 1.0;
        // 更新flag以便updateHealth 直接过去一年
        robinson.flag = 4;

        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                // 心情极好 体力减半 解锁星期五
                if (success) {
                    robinson.setMood(Robinson.M_MAX);
                    robinson.setStrength(Robinson.S_MAX);
                    panel.add(fight);
                    panel.repaint();
                    try {
                        Thread.sleep(3500);
                    } catch (InterruptedException interruptedException) {
                        interruptedException.printStackTrace();
                    }
                    panel.repaint();
                    panel.remove(fight);
                    panel.add(label);
                    panel.add(friday);
                    panel.repaint();
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException interruptedException) {
                        interruptedException.printStackTrace();
                    }
                    panel.repaint();
                    robinson.gotFriday = true;
                    System.out.println("success");
                }
                // 被野人抓住并吃掉
                else {
                    robinson.live = false;
                    panel.add(fight);
                    panel.repaint();
                    try {
                        Thread.sleep(3500);
                    } catch (InterruptedException interruptedException) {
                        interruptedException.printStackTrace();
                    }
                    panel.repaint();
                }
                frame.dispose();

            }
        });
        t.start();
        return success;
    }

    /**
     * 鲁滨逊上岛的第27年，救出一个西班牙人和星期五的父亲
     * 成功与否取决于体力和运气
     *
     *
     * @param robinson  鲁滨逊实例，提供数据
     * @return 是否成功
     */
    static boolean saveFather(Robinson robinson) {
        JFrame frame = new JFrame();
        frame.setBounds(0, 0, Settings.frame_w, Settings.frame_h);
        frame.setLayout(null);
        frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        JPanel panel = new JPanel();
        panel.setBounds(0, 0, Settings.frame_w, Settings.frame_h);
        panel.setLayout(null);
        frame.add(panel);
        frame.setVisible(true);

        BufferedImage save_father = null;
        java.net.URL save_father_imgURL = SubPanels.class.getResource("imgs/father.png");
        try {
            save_father = ImageIO.read(new URL(save_father_imgURL.toString()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        ImagePanel father = new ImagePanel(save_father);

        father.setBounds(0, 0, Settings.frame_w, Settings.frame_h);

        java.net.URL fight2_imgURL = SubPanels.class.getResource("imgs/fight2.gif");
        JLabel fight = new JLabel(new ImageIcon(fight2_imgURL));
        fight.setBounds(0, 0, Settings.frame_w, Settings.frame_h);
        System.out.println("Robinson decided to save these people.");
        // 计算成功率
        boolean success = robinson.getStrength() * 0.0095 + Math.random() > 1.0;
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                // 心情极好 体力减半 解锁星期五
                if (success) {
                    System.out.println("Robinson saved these people successfully!");
                    robinson.setMood(Robinson.M_MAX);
                    robinson.setStrength(Robinson.S_MAX);
                    panel.add(fight);
                    panel.repaint();
                    try {
                        Thread.sleep(3500);
                    } catch (InterruptedException interruptedException) {
                        interruptedException.printStackTrace();
                    }
                    panel.repaint();
                    panel.remove(fight);
                    panel.add(father);
                    panel.repaint();
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException interruptedException) {
                        interruptedException.printStackTrace();
                    }
                    panel.repaint();
                    robinson.gotFriday = true;
                }
                // 被野人抓住并吃掉
                else {
                    System.out.println("Robinson were killed!");
                    robinson.live = false;
                    panel.add(fight);
                    panel.repaint();
                    try {
                        Thread.sleep(3500);//该线程睡眠1.5秒
                    } catch (InterruptedException interruptedException) {
                        interruptedException.printStackTrace();
                    }
                    panel.repaint();
                }
                frame.dispose();

            }
        });
        t.start();//启动线程
        return success;
    }

    /**
     * 鲁滨逊夺船回国
     * @param robinson 鲁滨逊实例
     * @return 是否成功
     */
    static boolean leaveFrame(Robinson robinson) {

        JFrame frame = new JFrame();
        frame.setBounds(0, 0, Settings.frame_w, Settings.frame_h);
        frame.setLayout(null);
        frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        JPanel panel = new JPanel();
        panel.setBounds(0, 0, Settings.frame_w, Settings.frame_h);
        panel.setLayout(null);
        frame.add(panel);
        frame.setVisible(true);

        JLabel label = new JLabel();
        label.setBounds(50, 100, 600, 200);
        label.setText("<html>Robinson<br> going back home <br>happily</html>");
        label.setForeground(Color.BLACK);
        label.setFont(new java.awt.Font(Settings.button_font, Font.BOLD, Settings.getFont_size() + 3));

        BufferedImage back_home = null;
        java.net.URL back_imgURL = SubPanels.class.getResource("imgs/back.png");
        try {
            back_home = ImageIO.read(new URL(back_imgURL.toString()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        ImagePanel back = new ImagePanel(back_home);

        back.setBounds(0, 0, Settings.frame_w, Settings.frame_h);

        java.net.URL fight3_imgURL = SubPanels.class.getResource("imgs/fight3.gif");
        JLabel fight = new JLabel(new ImageIcon(fight3_imgURL));
        fight.setBounds(0, 0, Settings.frame_w, Settings.frame_h);
        System.out.println("back home");
        // 计算成功率
        boolean success = robinson.getStrength() * 0.0099 + Math.random() > 1.0;
        // 更新flag以便updateHealth 直接过去一年
        robinson.flag = 4;

        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                // 心情极好 体力减半 解锁星期五
                if (success) {
                    robinson.setMood(Robinson.M_MAX);
                    panel.add(fight);
                    panel.repaint();
                    try {
                        Thread.sleep(4000);
                    } catch (InterruptedException interruptedException) {
                        interruptedException.printStackTrace();
                    }
                    panel.repaint();
                    panel.remove(fight);
                    panel.add(label);
                    panel.add(back);
                    panel.repaint();
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException interruptedException) {
                        interruptedException.printStackTrace();
                    }
                    panel.repaint();
                    System.out.println("success");
                }
                else {
                    robinson.live = false;
                    panel.add(fight);
                    panel.repaint();
                    try {
                        Thread.sleep(3500);
                    } catch (InterruptedException interruptedException) {
                        interruptedException.printStackTrace();
                    }
                    panel.repaint();
                }
                frame.dispose();

            }
        });
        t.start();
        return success;
    }
}


